<template>
  <div class="app-container">
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="80%" v-dialog-drag append-to-body>
      <div style="padding: 5px 10px; margin-bottom: 20px;">
        <el-row>
          <el-col :span="6">
            任务编号：{{ pickTask.code }}
          </el-col>
          <el-col :span="6">
            发货单号：{{ pickTask.shipOrderCode }}
          </el-col>
          <el-col :span="6">
            状态：{{ getDictDataLabel(DICT_TYPE.PICK_TASK_STATUS, pickTask.status) }}
          </el-col>
          <el-col :span="6">
            分配给：{{ pickTask.assignUserName }}
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column type="index" label="序号" align="center" width="50" />
        <el-table-column label="物料代码" align="center" prop="materialModeCode" />
        <el-table-column label="物料名称" align="center" prop="materialName" />
        <el-table-column label="数量" align="center" prop="materialCount" />
        <el-table-column label="状态" align="center" prop="status">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.PICK_TASK_STATUS" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="已拣数量" align="center" prop="pickCount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button v-if="scope.row.status === 'PICKING'" size="mini" type="text" icon="el-icon-edit" @click="openStartPickList(scope.row)"
                       v-hasPermi="['hs:pick-task-material:update']">继续拣货</el-button>
            <el-button v-else-if="scope.row.status === 'TO_BE_PICKED'" size="mini" type="text" icon="el-icon-thumb" @click="openStartPickList(scope.row)"
                       v-hasPermi="['hs:pick-task-material:update']">开始拣货</el-button>
            <el-button v-else-if="scope.row.status === 'PICK_OVER'" size="mini" type="text" icon="el-icon-info" @click="openPickTaskMaterialDetailList(scope.row)"
                       v-hasPermi="['hs:pick-task-material:query']">已拣明细</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmAllPick" :disabled="disabled" size="medium">确认全部拣完</el-button>
      </div>
    </el-dialog>
    <PickTaskMaterialDetailList ref="pickTaskMaterialDetailListRef" @success="getList(pickTask.id)" />
    <PickScanForm ref="pickScanFormRef" @success="getList(pickTask.id)" />
  </div>
</template>
<script>
import {getPickTaskMaterialList, getStartPick} from "@/api/hs/picktaskmaterial";
import {complete, getPickTask} from "@/api/hs/picktask";
import {DICT_TYPE} from "@/utils/dict";
import StartPick from "@/views/hs/componnets/StartPick.vue";
import PickScanForm from "@/views/hs/componnets/PickScanForm.vue";
import PickTaskMaterialDetailList from "@/views/hs/componnets/PickTaskMaterialDetailList.vue";
import {getWarehouse} from "@/api/hs/warehouse";
import store from "@/store";

export default {
  name: "PickTaskMaterialList",
  components: {PickTaskMaterialDetailList, StartPick, PickScanForm},
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    }
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料信息列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 拣货任务
      pickTask: {},

      disabled: true,
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(pickTaskId) {
      this.list = [];
      this.pickTask = {};
      this.dialogVisible = true;
      this.dialogTitle = "拣货任务清单";
      await this.getList(pickTaskId);
    },
    /** 查询列表 */
    async getList(pickTaskId) {
      try {
        this.loading = true;
        await this.getPickTask(pickTaskId);
        const res = await getPickTaskMaterialList(pickTaskId)
        this.list = res.data;
        let flg = false;
        this.list.forEach(item => {
          if (item.pickCount < item.materialCount) {
            flg = true;
          }
        });
        this.disabled = flg;
      } finally {
        this.loading = false;
      }
    },
    /** 获取拣货任务信息 */
    async getPickTask(pickTaskId) {
      try {
        this.loading = true;
        const res = await getPickTask(pickTaskId)
        this.pickTask = res.data;
      } finally {
        this.loading = false;
      }
    },
    /** 开始拣货 */
    async openStartPickList(pickTaskMaterial) {
      const res = await getWarehouse(store.getters.warehouseId);
      if (res.data.firstInOut) {
        await this.$refs["startPickRef"].open(pickTaskMaterial.id)
      } else {
        const getStartPickRes = await getStartPick(pickTaskMaterial.id);
        await this.$refs["pickScanFormRef"].open("", getStartPickRes.data)
      }
    },
    /** 获取已拣明细 */
    openPickTaskMaterialDetailList(row) {
      this.$refs["pickTaskMaterialDetailListRef"].open(this.pickTask.id, row.materialModeCode);
    },
    /** 确认全部拣完 */
    async confirmAllPick() {
      await complete(this.pickTask.id).then(res => {
        if (res.data) {
          this.$message.success("确认全部拣完成功");
          this.dialogVisible = false;
        }
      }).finally(() => {
        this.$emit("success");
      })
    },
  }
}
</script>
<style scoped lang="scss">

</style>
